package com.qdlc.p2p.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.qdlc.p2p.biz.service.ExperienceBorrowService;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.ExperienceBorrowDao;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.ExperienceBorrow;
import com.qdlc.p2p.dal.model.ExperienceBorrowModel;

/**
 * 体验标service实现类
 *
 * @author xxx
 * @version 2.0
 * @date 2015年4月23日 下午8:02:57
 */
@Repository("experienceBorrowService")
public class ExperienceBorrowServiceImpl implements ExperienceBorrowService {
    private Logger logger = Logger.getLogger(ExperienceBorrowServiceImpl.class);

    @Resource
    private ExperienceBorrowDao experienceBorrowDao;

    @Override
    public PageDataList<ExperienceBorrowModel> findByModel(ExperienceBorrowModel model) {
        QueryParam param = QueryParam.getInstance();
        if (!StringUtil.isBlank(model.getSearchName())) {//模糊查询条件
            param.addParam("borrow.name", Operators.LIKE, model.getSearchName().trim());
        }
        param.addPage(model.getPage(), model.getRows());
        param.addOrder(OrderType.DESC, "id");
        PageDataList<ExperienceBorrow> pageDataList = experienceBorrowDao.findPageList(param);
        PageDataList<ExperienceBorrowModel> pageDataList_ = new PageDataList<ExperienceBorrowModel>();
        List<ExperienceBorrowModel> list = new ArrayList<ExperienceBorrowModel>();
        pageDataList_.setPage(pageDataList.getPage());
        if (pageDataList.getList().size() > 0) {
            for (ExperienceBorrow experienceBorrow : pageDataList.getList()) {
                ExperienceBorrowModel experienceBorrowModel = ExperienceBorrowModel.instance(experienceBorrow);
                experienceBorrowModel.setBorrowName(experienceBorrow.getBorrow().getName());
                list.add(experienceBorrowModel);
            }
        }
        pageDataList_.setList(list);
        return pageDataList_;
    }

    @Override
    public void save(ExperienceBorrow experienceBorrow) {
        experienceBorrowDao.save(experienceBorrow);
    }

    @Override
    public void save(Borrow borrow, double mostExperience,
                     double mostExperienceSingle) {
        if (borrow != null) {
            ExperienceBorrow experienceBorrow = new ExperienceBorrow();
            experienceBorrow.setBorrow(borrow);
            experienceBorrow.setAccount(borrow.getAccount());
            if (mostExperience < mostExperienceSingle) {
                throw new BusinessException("体验券投标最大额度不能小于单人体验券投标最大额度！");
            }
            experienceBorrow.setMostExperience(mostExperience);
            experienceBorrow.setMostExperienceSingle(mostExperienceSingle);
            experienceBorrowDao.save(experienceBorrow);
            logger.info("体验标信息保存成功，借款标ID：" + borrow.getId());
        }
    }

    @Override
    public ExperienceBorrow findById(long id) {
        return experienceBorrowDao.find(id);
    }

    @Override
    public ExperienceBorrow findByBorrow(long borrowId) {
        return experienceBorrowDao.findObjByProperty("borrow.id", borrowId);
    }

    @Override
    public void update(ExperienceBorrow experienceBorrow) {
        experienceBorrowDao.update(experienceBorrow);
    }

}
